import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.DBConnection;

/**
 * This servlet is used to handle requests for showing pubs selecting from the database.
 * 
 * @author Josef Hardi
 *
 */
public class ShowPub extends HttpServlet {

	/** Database connection **/
	private Connection conn = null;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		
		int id = Integer.parseInt(request.getParameter("id"));
		
		// Get the database connection
		conn = DBConnection.getConnection();
		
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
  	  	try {
  	  		String query = "SELECT id, name, description, facility, latitude, longitude, open_hour, close_hour, address " +
  	  					   "FROM Pub " +
  	  				       "WHERE id = ?";
  	  		stmt = conn.prepareStatement(query);
  	  		stmt.setInt(1, id);
  	  		
  	  		// Execute select operation
  	  		rs = stmt.executeQuery();
  	  		
  	  		if (rs.next()) {
  	  		
  	  			// ID
  	  			request.setAttribute("id", rs.getInt("id"));
  	  		
	  	  		// Name
				request.setAttribute("name", rs.getString("name"));
				
				// Description
				request.setAttribute("description", rs.getString("description"));
				
				// Facility
				String facilities = rs.getString("facility");
				StringTokenizer facilityTokens = new StringTokenizer(facilities, ",");
				while (facilityTokens.hasMoreTokens()) {
					String facility = facilityTokens.nextToken().trim();
					
					if (facility.equals("Beer"))
						request.setAttribute("has_beer", "checked");
					else if (facility.equals("Live music"))
						request.setAttribute("has_livemusic", "checked");
					else if (facility.equals("Dance floor"))
						request.setAttribute("has_dancefloor", "checked");
					else if (facility.equals("No smoking"))
						request.setAttribute("has_nosmoking", "checked"); 
				}
				
				// Latitude and Longitude
				double latitude = (double) (rs.getInt("latitude") / 1E6);
				double longitude = (double) (rs.getInt("longitude") / 1E6);
				request.setAttribute("latitude", latitude);
				request.setAttribute("longitude", longitude);
				
				StringTokenizer timeTokens = null;
				SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
				// Open Time
				String openTime = new StringBuilder(timeFormat.format(rs.getTime("open_hour"))).toString();
				timeTokens = new StringTokenizer(openTime, ":");
				request.setAttribute("open_hour_" + timeTokens.nextToken(), "selected");  // hour
				request.setAttribute("open_minute_" + timeTokens.nextToken(), "selected");  //minute
				
				// Close Time
				String closeTime = new StringBuilder(timeFormat.format(rs.getTime("close_hour"))).toString();
				timeTokens = new StringTokenizer(closeTime, ":");
				request.setAttribute("close_hour_" + timeTokens.nextToken(), "selected");  // hour
				request.setAttribute("close_minute_" + timeTokens.nextToken(), "selected");  //minute
				
				// Address
				request.setAttribute("address", rs.getString("address"));
			
  	  		}
  	  		
			RequestDispatcher rd=request.getRequestDispatcher("edit.jsp");  
            rd.forward(request, response);
  	  	}
  	  	catch (SQLException e) {
  	  		e.printStackTrace();
  	  	}
  	  	finally {
	    	try {
	           // Close everything.
	    	   if (rs != null) rs.close();
	           if (stmt!= null) stmt.close();
	           if (conn!= null) conn.close();
	    	} 
	    	catch (Exception e) { // Trap all other exceptions
	    			e.printStackTrace();
	    	}
	   }	
	}
	
}
